<?php

namespace App\Entities;

use App\Events\ArticleCreated;
use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

class Article extends Model implements Transformable
{
    use TransformableTrait;

    public $table = 'articles';

    protected $fillable=[
        'category_id','title','keys','flag_id','clicks','user_id','photo','intro','content','code',
    ];

    protected $dates = ['deleted_at'];

    protected static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        //软删除关联模型
        self::deleted(function($model){
           $model->articleTag()->detach();
        });
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function articleTag(){
        return $this->belongsToMany('App\Entities\ArticleTag', 'article_tags');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function articleCategory(){
        return $this->hasOne('App\Entities\Category','id','category_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function articleBackUser(){
        return $this->hasOne('App\Entities\Back\Back' ,'id' ,'user_id');
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function articleArticleTag(){
        return $this->hasMany('App\Entities\ArticleTag', 'article_id', 'id');
    }

    protected $events = [
        'created' => ArticleCreated::class,
    ];
}
